<template>
  <div style="padding:20px 20px 20px 40px">
    <a-input v-model:value="searchValue"
             style="margin-bottom: 8px; border-radius: 30px;width: 200px" placeholder="搜索"/>
    <a-tree
        :expanded-keys="expandedKeys"
        :auto-expand-parent="autoExpandParent"
        :tree-data="gData"
        @expand="onExpand"
    >
      <template #title="{ key: treeKey, title,level }">
        <a-dropdown :trigger="['contextmenu']">
          <span v-if="title.indexOf(searchValue) > -1">
          <div v-if="level==1" class="right-btn1" @click="btn1Click(title)">{{ title }}</div>
               <div v-if="level==2" class="right-btn2" @click="btn2Click(title)">{{ title }}</div>
               <div v-if="level==3" class="right-btn3" @click="btn3Click(title)">{{ title }}</div>
          </span>

          <template #overlay>
            <a-menu @click="({ key: menuKey }) => onContextMenuClick(treeKey, menuKey)">
              <a-menu-item key="1">创建</a-menu-item>
              <a-menu-item key="2">重命名</a-menu-item>
              <a-menu-item key="3">删除</a-menu-item>
            </a-menu>
          </template>
        </a-dropdown>
      </template>

      <!--      <template #title="{ title }">-->
      <!--        <span v-if="title.indexOf(searchValue) > -1">-->
      <!--              {{ title }}-->
      <!--                    {{ title.substr(0, title.indexOf(searchValue)) }}-->
      <!--                    <span style="color: #f50">{{ searchValue }}</span>-->
      <!--                    {{ title.substr(title.indexOf(searchValue) + searchValue.length) }}-->
      <!--        </span>-->
      <!--        <span v-else>{{ title }}</span>-->
      <!--      </template>-->
    </a-tree>
  </div>
</template>
<script lang="ts">

import {defineComponent, ref, watch} from 'vue';
import type {TreeProps} from 'ant-design-vue';

const x = 3;
const y = 2;
const z = 1;
const genData: TreeProps['treeData'] = [];

const generateData = (_level: number, _preKey?: string, _tns?: TreeProps['treeData']) => {
  const preKey = _preKey || '0';
  const tns = _tns || genData;

  const children = [];
  for (let i = 0; i < x; i++) {
    const key = `${preKey}-${i}`;
    tns.push({title: key, key});
    if (i < y) {
      children.push(key);
    }
  }
  if (_level < 0) {
    return tns;
  }
  const level = _level - 1;
  children.forEach((key, index) => {
    tns[index].children = [];
    return generateData(level, key, tns[index].children);
  });
};
generateData(z);

const dataList: TreeProps['treeData'] = [];
const generateList = (data: TreeProps['treeData']) => {
  for (let i = 0; i < data.length; i++) {
    const node = data[i];
    const key = node.key;
    dataList.push({key, title: key});
    if (node.children) {
      generateList(node.children);
    }
  }
};
generateList(genData);

const getParentKey = (
    key: string | number,
    tree: TreeProps['treeData'],
): string | number | undefined => {
  let parentKey;
  for (let i = 0; i < tree.length; i++) {
    const node = tree[i];
    if (node.children) {
      if (node.children.some(item => item.key === key)) {
        parentKey = node.key;
      } else if (getParentKey(key, node.children)) {
        parentKey = getParentKey(key, node.children);
      }
    }
  }
  return parentKey;
};
export default defineComponent({
  setup() {
    const expandedKeys = ref<(string | number)[]>([]);
    const searchValue = ref<string>('');
    const autoExpandParent = ref<boolean>(true);
    console.log(genData)
    const treeData = [

      {
        title: "模型",
        key: "模型",
        level: 1,
        children: [
          {title: "大唐不夜城", key: "大唐不夜城", level: 2},
          {title: "大唐宾馆", key: "大唐宾馆", level: 2},
        ],
      },
      {
        title: "驻地",
        key: "驻地",
        level: 1,
        children: [
          {title: "倾斜模型", key: "倾斜模型", level: 2},
          {title: "三区划分", key: "三区划分", level: 2},
          {title: "警力部署", key: "警力部署", level: 2},
          {title: "无人机反制", key: "无人机反制", level: 2},
          {
            title: "重点部位",
            key: "重点部位", level: 2,

            children: [
              {title: "加油站", key: "加油站", level: 3},
              {title: "加气站", key: "加气站", level: 3},
              {title: "应急避险", key: "应急避险", level: 3},
            ],
          },
          {title: "任务流线", key: "任务流线", level: 2},
        ],
      },


      // {title: '0-0', key: '0-0', children: []},
      //
      // {title: '0-1', key: '0-1', children: []}
      // ,
      // {title: '0-2', key: '0-2'}
    ]
    const gData = ref<TreeProps['treeData']>(treeData);

    const onExpand = (keys: string[]) => {
      expandedKeys.value = keys;
      autoExpandParent.value = false;
    };

    watch(searchValue, value => {
      const expanded = dataList
          .map((item: TreeProps['treeData'][number]) => {
            if (item.title.indexOf(value) > -1) {
              return getParentKey(item.key, gData.value);
            }
            return null;
          })
          .filter((item, i, self) => item && self.indexOf(item) === i);
      expandedKeys.value = expanded;
      searchValue.value = value;
      autoExpandParent.value = true;
    });
    return {
      expandedKeys,
      searchValue,
      autoExpandParent,
      gData,
      onExpand,
    };
  },
});
</script>
<style>
.ant-tree {
  background: transparent;
  color: #fff
}

.right-btn1 {
  width: 149px;
  height: 69px;
  background: url("/imgs/一级按钮未选中切图.png") no-repeat 100%;
  text-align: center;
  line-height: 70px;

  cursor: pointer;
}

.right-btn1:hover {
  background: url("/imgs/一级按钮选中切图.png") no-repeat 100%;
}

.right-btn1_h {
  width: 149px;
  height: 69px;
  background: url("/imgs/一级按钮选中切图.png") no-repeat 100%;
  text-align: center;
  line-height: 70px;

  cursor: pointer;
}

.right-btn2 {
  width: 110px;
  height: 42px;
  background: url("/imgs/二级标题未选中切图.png");
  text-align: center;
  line-height: 36px;
  margin: 0px 30px 10px;

  cursor: pointer;
}

.right-btn2:hover {
  background: url("/imgs/二级按钮选中切图.png");
}

.right-btn2_h {
  width: 110px;
  height: 42px;
  background: url("/imgs/二级按钮选中切图.png");
  text-align: center;
  line-height: 36px;
  margin: 0px 30px 10px;
  cursor: pointer;
}

.right-btn3 {
  width: 101px;
  height: 32px;
  background: url("/imgs/三级标题未选中按钮切图.png");
  text-align: center;
  line-height: 32px;
  margin: 0px 60px 10px;

  cursor: pointer;
}

.right-btn3:hover {
  background: url("/imgs/三级标题选中切图.png");
}

.right-btn3_h {
  width: 101px;
  height: 32px;
  background: url("/imgs/三级标题选中切图.png");
  text-align: center;
  line-height: 32px;
  margin: 0px 60px 10px;
  cursor: pointer;
}

.ant-tree .ant-tree-node-content-wrapper.ant-tree-node-selected {
  background-color: transparent;
}

.ant-tree .ant-tree-node-content-wrapper:hover {
  background-color: transparent;
}
</style>